import axios from "axios";
import qs from "qs";
import { useUserStoreHook } from "@/store/modules/user.store";
import { ResultEnum } from "@/enums/api/result.enum";
import { getAccessToken } from "@/utils/auth";
import router from "@/router";

// 创建axios实例
const service = axios.create( {
    baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout: 50000,
    headers: {
        "Content-Type": "application/json;charset=utf-8",
        'Cache-Control': 'no-cache'
    },
    paramsSerializer: ( params ) => qs.stringify( params )
} );


// 请求拦截器
service.interceptors.request.use( ( config ) => {
    const accessToken = getAccessToken();
    if ( config.headers.Authorization !== 'no-auth' && accessToken ) {
        config.headers.Authorization = `Bearer ${accessToken}`;
    } else {
        delete config.headers.Authorization;
    }
    return config;

},
    ( error ) => { Promise.reject(); } );

export default service;
